import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import React, { useMemo } from "react";
import classes from "./phoneEmailBind.module.less";
import PhoneInput from "../../../compoents/phone-input";
import VerificationCode from "../../../compoents/verifiaction-code";
import classNames from "classnames";
import LinearInput from "../../../compoents/linear-input";
import { useLockFn, useSetState } from "ahooks";
import useServices from "../../../hooks/useRequest";
import { phoneEmailBind } from "../../../api/auth";
import kingsInstance from "../../../instance";
import utilsBase from "../../../utils/basc";
import toast from "../../../compoents/toast";
import { useTranslation } from "react-i18next";
import { LocalKeyMap } from "../../../local";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
function PhoneEmailBind(props) {
    var _useSetState = _sliced_to_array(useSetState({
        phone: "",
        email: "",
        area: "",
        code: ""
    }), 2), state = _useSetState[0], setState = _useSetState[1];
    var services = useServices({
        phoneEmailBind: phoneEmailBind
    });
    var t = useTranslation().t;
    var enableBtn = useMemo(function() {
        if (props.type === "mail") {
            return state.email && state.code;
        }
        return state.area && state.phone && state.code;
    }, [
        props.type,
        state.phone,
        state.code,
        state.email,
        state.area
    ]);
    var bind = useLockFn(/*#__PURE__*/ _async_to_generator(function() {
        var data;
        return _ts_generator(this, function(_state) {
            if (!enableBtn) return [
                2
            ];
            data = {
                // password: state.password,
                email: undefined,
                areaCode: undefined,
                loginName: undefined,
                code: state.code,
                onaAppId: kingsInstance.authConfig.ONA_APPID,
                onaAppSecret: kingsInstance.authConfig.ONA_APP_SECRET
            };
            if (props.type === "mail") {
                if (!utilsBase.checkEmail(state.email)) {
                    toast.createToast({
                        type: "errToast",
                        message: t(LocalKeyMap["toast.error.validEmail"])
                    });
                    return [
                        2
                    ];
                }
                data.loginName = state.email;
            } else {
                data.loginName = "".concat(state.area, "-").concat(state.phone);
            }
            return [
                2,
                services.phoneEmailBind(data).then(function(res) {
                    if (res.code == 200) {
                        //绑定成功
                        toast.createToast({
                            message: t(LocalKeyMap.successful)
                        });
                        props.onBind(res.data);
                    } else {
                        throw res;
                    }
                }).catch(function(err) {
                    toast.createToast({
                        type: "errToast",
                        message: err.msg
                    });
                })
            ];
        });
    }));
    return /*#__PURE__*/ _jsx("div", {
        className: classes.phone_email_bind,
        children: /*#__PURE__*/ _jsx("div", {
            className: classes.phone_email_bind_wrap,
            children: /*#__PURE__*/ _jsxs("div", {
                className: classes.form,
                children: [
                    /*#__PURE__*/ _jsxs("div", {
                        className: classes.label,
                        children: [
                            /*#__PURE__*/ _jsx("div", {
                                className: classes.text,
                                children: props.type === "phone" ? t(LocalKeyMap.mobile) : t(LocalKeyMap.email)
                            }),
                            props.type === "phone" ? /*#__PURE__*/ _jsx(PhoneInput, {
                                value: state.phone,
                                onChange: function(area, phone) {
                                    setState({
                                        area: area,
                                        phone: phone
                                    });
                                },
                                maxlength: 15,
                                name: "phone"
                            }) : /*#__PURE__*/ _jsx(LinearInput, {
                                value: state.email,
                                onChange: function(value) {
                                    setState({
                                        email: value
                                    });
                                },
                                placeholder: t(LocalKeyMap.loginHeader31),
                                name: "email"
                            })
                        ]
                    }),
                    /*#__PURE__*/ _jsxs("div", {
                        className: classes.label,
                        children: [
                            /*#__PURE__*/ _jsx("div", {
                                className: classes.text,
                                children: t(LocalKeyMap.loginHeader35)
                            }),
                            /*#__PURE__*/ _jsx(VerificationCode, {
                                target: props.type === "phone" ? state.area + "-" + state.phone : state.email,
                                value: state.code,
                                onChange: function(value) {
                                    return setState({
                                        code: value
                                    });
                                }
                            })
                        ]
                    }),
                    /*#__PURE__*/ _jsx("div", {
                        onClick: bind,
                        className: classNames(classes.btn, enableBtn ? classes.btn_enable : ""),
                        children: /*#__PURE__*/ _jsx("span", {
                            children: t(LocalKeyMap.confirm)
                        })
                    })
                ]
            })
        })
    });
}
export default PhoneEmailBind;

 //# sourceMappingURL=index.js.map